package gui;

import engine.DefaultOntologyManager;
import engine.LDAPManager;
import engine.OntologyManager;
import engine.ELOntologyManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Pospishniy Olexandr
 */
public class MainFrame extends javax.swing.JFrame {

	/**
	 * Creates new form MainFrame
	 */
	public MainFrame() {
		initComponents();
		statusLabel.setVisible(false);
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                jPanel1 = new javax.swing.JPanel();
                ldapServerField = new javax.swing.JTextField();
                jLabel1 = new javax.swing.JLabel();
                jLabel2 = new javax.swing.JLabel();
                ldapPortField = new javax.swing.JTextField();
                anonymousCheckBox = new javax.swing.JCheckBox();
                jPanel2 = new javax.swing.JPanel();
                jLabel3 = new javax.swing.JLabel();
                jLabel4 = new javax.swing.JLabel();
                glueOntologyField = new javax.swing.JTextField();
                browseGLUE = new javax.swing.JButton();
                outputOntologyField = new javax.swing.JTextField();
                browseOutput = new javax.swing.JButton();
                jLabel5 = new javax.swing.JLabel();
                ontologyIRI = new javax.swing.JTextField();
                elCheckBox = new javax.swing.JCheckBox();
                jPanel3 = new javax.swing.JPanel();
                importSites = new javax.swing.JCheckBox();
                importServices = new javax.swing.JCheckBox();
                importSEs = new javax.swing.JCheckBox();
                importCEs = new javax.swing.JCheckBox();
                importVOViews = new javax.swing.JCheckBox();
                importClusters = new javax.swing.JCheckBox();
                importSubClusters = new javax.swing.JCheckBox();
                startButton = new javax.swing.JButton();
                statusLabel = new javax.swing.JProgressBar();

                setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

                jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("LDAP settings"));

                ldapServerField.setText("lcg-bdii.cern.ch");

                jLabel1.setText("LDAP server");

                jLabel2.setText("port");

                ldapPortField.setText("2170");

                anonymousCheckBox.setSelected(true);
                anonymousCheckBox.setText("Anonymous access");

                javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
                jPanel1.setLayout(jPanel1Layout);
                jPanel1Layout.setHorizontalGroup(
                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(ldapServerField, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jLabel2)
                                                .addGap(18, 18, 18)
                                                .addComponent(ldapPortField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(anonymousCheckBox, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addContainerGap())
                );
                jPanel1Layout.setVerticalGroup(
                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(ldapServerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(ldapPortField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(anonymousCheckBox)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );

                jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Ontologies"));

                jLabel3.setText("GLUE ontology");

                jLabel4.setText("Output ontology");

                glueOntologyField.setText("D:\\test\\T-box.owl");

                browseGLUE.setText("Browse");
                browseGLUE.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                browseGLUEActionPerformed(evt);
                        }
                });

                browseOutput.setText("Browse");
                browseOutput.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                browseOutputActionPerformed(evt);
                        }
                });

                jLabel5.setText("Output ontology IRI");

                ontologyIRI.setText("http://localhost/ontologies/GLUE/lcg.owl");

                elCheckBox.setText("GLUE is OWL 2 EL Ontology");
                elCheckBox.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                elCheckBoxActionPerformed(evt);
                        }
                });

                javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
                jPanel2.setLayout(jPanel2Layout);
                jPanel2Layout.setHorizontalGroup(
                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                                .addComponent(glueOntologyField)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(browseGLUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(ontologyIRI, javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(outputOntologyField))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(browseOutput))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(elCheckBox)
                                                        .addComponent(jLabel3)
                                                        .addComponent(jLabel4)
                                                        .addComponent(jLabel5))
                                                .addGap(0, 0, Short.MAX_VALUE)))
                                .addContainerGap())
                );
                jPanel2Layout.setVerticalGroup(
                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(glueOntologyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(browseGLUE))
                                .addGap(9, 9, 9)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(outputOntologyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(browseOutput))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ontologyIRI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                                .addComponent(elCheckBox)
                                .addContainerGap())
                );

                jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Import settings"));

                importSites.setText("Import Sites");

                importServices.setText("Import Services");

                importSEs.setText("Import SEs");

                importCEs.setText("Import CEs");

                importVOViews.setText("Import VOViews");

                importClusters.setText("Import Clusters");

                importSubClusters.setText("Import SubClusters/Hosts");

                javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
                jPanel3.setLayout(jPanel3Layout);
                jPanel3Layout.setHorizontalGroup(
                        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(importSites)
                                        .addComponent(importServices)
                                        .addComponent(importSEs)
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(importClusters)
                                                        .addComponent(importCEs))
                                                .addGap(18, 18, 18)
                                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(importVOViews)
                                                        .addComponent(importSubClusters))))
                                .addContainerGap(114, Short.MAX_VALUE))
                );
                jPanel3Layout.setVerticalGroup(
                        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(importSites)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(importServices)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(importCEs)
                                        .addComponent(importVOViews))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 3, Short.MAX_VALUE)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(importClusters)
                                        .addComponent(importSubClusters))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(importSEs)
                                .addGap(10, 10, 10))
                );

                startButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
                startButton.setText("Start");
                startButton.setEnabled(false);
                startButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                startButtonActionPerformed(evt);
                        }
                });

                statusLabel.setBackground(new java.awt.Color(204, 255, 204));
                statusLabel.setIndeterminate(true);

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(startButton)))
                                .addContainerGap())
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(startButton))
                                .addContainerGap())
                );

                pack();
        }// </editor-fold>//GEN-END:initComponents

    private void browseGLUEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseGLUEActionPerformed
	    try {
		    JFileChooser fileChooser = new JFileChooser();
		    fileChooser.showOpenDialog(this);
		    if (fileChooser.getSelectedFile() != null) {
			    glueOntologyField.setText(fileChooser.getSelectedFile().getCanonicalPath());
		    }
	    } catch (IOException ex) {
		    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    if (glueOntologyField.getText() != null && outputOntologyField.getText() != null) {
		    startButton.setEnabled(true);
	    }
    }//GEN-LAST:event_browseGLUEActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
	    Thread thread = new Thread(new Runnable() {
		    public void run() {
			    statusLabel.setVisible(true);
			    startButton.setEnabled(false);
			    OntologyManager ontologyManager;
			    if (elCheckBox.isSelected()) {
				    ontologyManager = new ELOntologyManager(glueOntologyField.getText(), outputOntologyField.getText(), ontologyIRI.getText());
			    } else {
				    ontologyManager = new DefaultOntologyManager(glueOntologyField.getText(), outputOntologyField.getText(), ontologyIRI.getText());
			    }
			    ontologyManager.createNewEmptyOntology();
			    LDAPManager ldapm = new LDAPManager(ldapServerField.getText(), ldapPortField.getText());
			    try {
				    if (importSites.isSelected()) {
					    ontologyManager.saveSites(ldapm.retriveSites());
				    }
				    if (importServices.isSelected()) {
					    ontologyManager.saveServices(ldapm.retriveServices());
				    }
				    if (importCEs.isSelected()) {
					    ontologyManager.saveCEs(ldapm.retriveCEs());
				    }
				    if (importVOViews.isSelected()) {
					    ontologyManager.saveVOViews(ldapm.retriveVOViews());
				    }
				    if (importClusters.isSelected()) {
					    ontologyManager.saveClusters(ldapm.retriveClusters());
				    }
				    if (importSubClusters.isSelected()) {
					    ontologyManager.saveSubClusters(ldapm.retriveSubClusters());
				    }
				    if (importSEs.isSelected()) {
					    ontologyManager.saveSEs(ldapm.retriveSEs());
				    }
				    System.out.println("All done");
			    } catch (NamingException ex) {
				    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
			    }
			    statusLabel.setVisible(false);
			    startButton.setEnabled(true);
		    }
	    });
	    thread.start();
    }//GEN-LAST:event_startButtonActionPerformed

    private void browseOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseOutputActionPerformed
	    try {
		    JFileChooser fileChooser = new JFileChooser();
		    fileChooser.showSaveDialog(this);
		    if (fileChooser.getSelectedFile() != null) {
			    outputOntologyField.setText(fileChooser.getSelectedFile().getCanonicalPath());
		    }
	    } catch (IOException ex) {
		    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    if (glueOntologyField.getText() != null && outputOntologyField.getText() != null) {
		    startButton.setEnabled(true);
	    }
    }//GEN-LAST:event_browseOutputActionPerformed

        private void elCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_elCheckBoxActionPerformed
		// TODO add your handling code here:
        }//GEN-LAST:event_elCheckBoxActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		try {
			//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (ClassNotFoundException ex) {
			Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
		} catch (UnsupportedLookAndFeelException ex) {
			Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
		}
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				MainFrame frame = new MainFrame();
				frame.setLocation(DesktopToolkit.getWindowLocation(frame.getSize()));
				frame.setVisible(true);
			}
		});
	}
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JCheckBox anonymousCheckBox;
        private javax.swing.JButton browseGLUE;
        private javax.swing.JButton browseOutput;
        private javax.swing.JCheckBox elCheckBox;
        private javax.swing.JTextField glueOntologyField;
        private javax.swing.JCheckBox importCEs;
        private javax.swing.JCheckBox importClusters;
        private javax.swing.JCheckBox importSEs;
        private javax.swing.JCheckBox importServices;
        private javax.swing.JCheckBox importSites;
        private javax.swing.JCheckBox importSubClusters;
        private javax.swing.JCheckBox importVOViews;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JTextField ldapPortField;
        private javax.swing.JTextField ldapServerField;
        private javax.swing.JTextField ontologyIRI;
        private javax.swing.JTextField outputOntologyField;
        private javax.swing.JButton startButton;
        private javax.swing.JProgressBar statusLabel;
        // End of variables declaration//GEN-END:variables
}
